﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Contents_Action
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id") Is Nothing Then
                hidID.Value = Request.QueryString("Id")
                BindDataContents(hidID.Value)
                txtPublisheddate.Text = Now.ToString("dd/MM/yyyy")
                LoadListHour(ddlbeHour)
                ddlbeHour.SelectedIndex = Now.Hour
                LoadListHour(ddleHour)
                ddleHour.SelectedIndex = 23
                LoadListMinutes(ddlbeMinutes)
                ddlbeMinutes.SelectedIndex = Now.Minute
                LoadListMinutes(ddleMinutes)
                ddleMinutes.SelectedIndex = 59
                'Load list action status
                LoadListActionStatus()
            End If
        End If
        Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
        If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
            ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
        Else
            Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
        End If
    End Sub
    'Show du lieu cua contest
    Sub BindDataContents(ByVal iContentId As Integer)
        Using data As New VSEntities
            Dim p = (From q In data.Contents Where q.Id = iContentId Select New With {.ID = q.Id,
                                                                                      .MenuId = q.MenuId,
                                                                                       .Title = q.Title,
                                                                                         .Modified = q.Modified,
                                                                                          .TypeId = q.TypeId,
                                                                                             .StatusName = q.Status.Title
                                                                                              }).ToList
            Dim strKey_Name() As String = {"Id", "Title", "TypeId", "Modified", "MenuId"}
            With grdShow
                .DataKeyNames = strKey_Name
                .DataSource = p
                .DataBind()
            End With
        End Using
    End Sub
    Protected Sub grdShow_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdShow.RowDataBound
        If e.Row.RowIndex >= 0 Then
            Dim lblModified As Label = CType(e.Row.FindControl("lblModified"), Label)
            lblModified.Text = IIf(Convert.ToDateTime(grdShow.DataKeys(e.Row.RowIndex)("Modified")).ToString("dd/MM/yyyy") = "01/01/9000", "", Convert.ToDateTime(grdShow.DataKeys(e.Row.RowIndex)("Modified")).ToString("dd/MM/yyyy"))

            Dim lblTypeContent As Label = CType(e.Row.FindControl("lblTypeContent"), Label)
            Dim intType As Integer = grdShow.DataKeys(e.Row.RowIndex)("TypeId")
            If intType = 1 Then
                lblTypeContent.Text = "Bài viết chuẩn"
            ElseIf intType = 2 Then
                lblTypeContent.Text = "Video"
            ElseIf intType = 3 Then
                lblTypeContent.Text = "Bình chọn"
            ElseIf intType = 4 Then
                lblTypeContent.Text = "Album ảnh"
            ElseIf intType = 5 Then
                lblTypeContent.Text = "Tip"
            End If
        End If
    End Sub
    Sub LoadListHour(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 23
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    Sub LoadListMinutes(ByVal ddlName As DropDownList)
        For i As Integer = 0 To 59
            Dim iTem As New ListItem(i.ToString(), i.ToString())
            ddlName.Items.Add(iTem)
        Next
    End Sub
    'Load list action status of current status
    Sub LoadListActionStatus()
        Dim iCurrentStatusId As Integer = Request.QueryString("CStatusId")
        Using Data As New VSEntities
            'Load action tuong ung voi current statusid
            Dim st = (From q In Data.StatusActions Where q.CurrentStatusId = iCurrentStatusId Select q Order By
                    q.NextStatusId).ToList()
            For Each i In st
                If HasPermission(Function_Name.Content, Session("RoleID"), i.CurrentStatusId, i.Audit) Then
                    Dim _item As New ListItem(i.Title, i.NextStatusId)
                    ddlAction.Items.Insert(0, _item)
                End If
            Next
            ddlAction.SelectedIndex = ddlAction.Items.IndexOf(ddlAction.Items.FindByValue(Request.QueryString("NStatusId")))
        End Using
    End Sub
    'Luu trang thai action cua contest
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim iContentId As Integer = Request.QueryString("Id")
        Using data As New VSEntities
            Dim p = (From q In data.Contents Where q.Id = iContentId Select q).SingleOrDefault
            Try
                p.PublishedDate = StringToDateTime(txtPublisheddate.Text.Trim(), ddlbeHour.SelectedValue & ":" & ddlbeMinutes.SelectedValue)
                p.bEffectiveDate = p.PublishedDate
                If (txtEeffectivedate.Text.Trim() = "") Then
                    'p.eEffectiveDate = StringToDateTime(txtPublisheddate.Text.Trim(), "23:59")
                Else
                    p.eEffectiveDate = StringToDateTime(txtEeffectivedate.Text.Trim(), ddleHour.SelectedValue & ":" & ddleMinutes.SelectedValue)
                End If
                p.StatusID = ddlAction.SelectedValue
                data.SaveChanges()

                'Ghi vao history changed status
                Dim oHis As New VSModel.ContentActionHistory
                oHis.FunctionId = Function_Name.Content
                oHis.ObjId = iContentId
                oHis.Creator = Session("Username")
                oHis.Created = Now
                oHis.PublishedDate = p.PublishedDate 'StringToDateTime(txtPublisheddate.Text.Trim(), ddlbeHour.SelectedValue & ":" & ddlbeMinutes.SelectedValue)
                If (txtEeffectivedate.Text.Trim() = "") Then
                    'p.eEffectiveDate = StringToDateTime(txtPublisheddate.Text.Trim(), "23:59")
                Else
                    oHis.eEffectiveDate = StringToDateTime(txtEeffectivedate.Text.Trim(), ddleHour.SelectedValue & ":" & ddleMinutes.SelectedValue)
                End If

                oHis.CurrentStatusId = Request.QueryString("CStatusId")
                oHis.ActionStatusId = ddlAction.SelectedValue
                oHis.Note = txtNote.Text.Trim()
                data.ContentActionHistories.AddObject(oHis)
                data.SaveChanges()
                Insert_App_Log("Changed Status Contents:" & p.Title & "", Function_Name.Content, Audit_Type.Edit, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                Excute_Javascript("Alertbox('Thay đổi trạng trạng thái nội dung thành công.');window.location ='../../Page/Contents/List.aspx?MenuId=" & grdShow.DataKeys(0)("MenuId").ToString & "';", Me.Page, True)
                'Response.Redirect("List.aspx?MenuId=" + grdShow.DataKeys(0)("MenuId").ToString)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Thay đổi trạng trạng thái contest thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx?MenuId=" + grdShow.DataKeys(0)("MenuId").ToString)
    End Sub
#End Region
End Class
